package com.zzxx.exam.service;

import com.zzxx.exam.entity.*;

import java.util.*;

/**
 *
 */
public class ExamServiceImpl implements ExamService{
    private EntityContext entityContext;
    private User user;
    public void setEntityContext(EntityContext entityContext) {
        this.entityContext = entityContext;
    }

    @Override
    public User login(int id, String pwd) throws IdOrPwdException {
        user= entityContext.getUserById(id);
        //System.out.println(users.getName());
        if(user == null){
            throw new IdOrPwdException("用户名不存在!");
        }
        if(!pwd.equals(user.getPassword())){
            throw new IdOrPwdException("密码错误!");
        }
        return user;
    }

    // 定义一个集合 List<QuestionInfo> paper, 用来存储试卷题目
    private List<QuestionInfo> paper = new ArrayList<>();

    public List<QuestionInfo> getPaper() {
        return paper;
    }

    @Override
    public ExamInfo start() {

        //1.生成一个试卷[每一个难度级别随机生成2道题目]
        //调用EntityContext来获得题目
        generatePaper();
        //2.封装一个考试信息返回ExamInfo
        // ExamInfo : title-自定 user->登陆过的(login赋值过了)
        ExamInfo examInfo = new ExamInfo();
        examInfo.setUser(user);
        examInfo.setTitle(entityContext.getTitle());
        examInfo.setTimeLimit(entityContext.getLimit());
        examInfo.setQuestionCount(paper.size());
        // timeLimit: 配置文件  questionCount: 生成试卷时记录的

        return examInfo;
    }

    //每个等级随机选择两道放入paper
    private void generatePaper() {//生成paper试卷
        int index = 0;
        Random random = new Random();
        for (int i = Question.LEVEL1; i <= Question.LEVEL10; i++) {//难度为1~10
            List<Question> questions = entityContext.getQuestionsByLevel(i);
            Question question1 = questions.remove(random.nextInt(questions.size()));
            Question question2 = questions.remove(random.nextInt(questions.size()));
            paper.add(new QuestionInfo(index++,question1));
            paper.add(new QuestionInfo(index++,question2));
        }
    }

    @Override
    public QuestionInfo getQuestion(int index) {//index是问题的题号
        return this.paper.get(index);
    }

    /**
     * 保存用户当前题目的答案
     * @param index
     * @param userAnswers
     */
    @Override
    public void saveUserAnswers(int index, List<Integer> userAnswers) {
        paper.get(index).setUserAnswers(userAnswers);
    }

    /**
     * 计算分数
     * @return
     */
    @Override
    public int examOver() {
        int score = 0;
        for (int i = 0; i < 20; i++) {
            List<Integer> userAnswers = paper.get(i).getUserAnswers();
            List<Integer> answers = paper.get(i).getQuestion().getAnswers();
            if(isEqual(userAnswers,answers)){
                score += 5;
            }
        }
        return score;
    }

    @Override
    public String getMsg() {
        return entityContext.LoadRules(entityContext.getConfigRules()).toString();
    }

    public boolean isEqual(List<Integer> list1,List<Integer> list2){
        if(list1.equals(list2)){
            return true;
        }
        return false;
    }

}
